<!--
 * @Description: 设备管理
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: 韩静雯 3294295230@qq.com
 * @LastEditTime: 2025-09-28 17:15:22
-->
<template>
  <div>
    <!-- 第一行  -->
    <div class="header">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :span="18">
            <el-form-item>
              <el-select clearable v-model="form.online_status" placeholder="请选择设备状态">
                <el-option label="在线" :value="1"></el-option>
                <el-option label="离线" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select clearable v-model="form.bind_status" placeholder="请选择设备绑定状态">
                <el-option label="未绑定" :value="0"></el-option>
                <el-option label="已绑定" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input clearable v-model="form.name" placeholder="请输入设备名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toSearch">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align: right;">
            <el-form-item>
              <el-button type="primary" @click="toAdd">录入</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 第二行  -->
    <div class="table">
      <el-table :data="deviceData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="serial_number" label="编号" width="180">
        </el-table-column>
        <el-table-column prop="name" label="名称">
        </el-table-column>
        <el-table-column prop="online_status" label="设备状态">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.online_status == 1" type="success">在线</el-tag>
              <el-tag v-else type="danger">离线</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="bind_status" label="绑定状态">
          <template v-slot="scope">
            <div>
              <el-tag v-if="scope.row.bind_status == 1" type="success">已绑定</el-tag>
              <el-tag v-else type="danger">未绑定</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <div>
              <el-link @click="toEdit(scope.row)" type="primary" style="margin-left: 16px;">
                修改
              </el-link>

              <el-link @click="toDelete(scope.row.id)" :underline="false" type="warning">删除</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-drawer @close="reset" size="50%" :title="title" :visible.sync="visible" direction="rtl"
      custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content" style="padding-right: 10px;">
        <!-- {{ this.formSave }}   -->
        <el-form :rules="rules" ref="myForm" :model="formSave">
          <el-row>
            <el-col :span="24">
              <el-form-item prop="serial_number" label="设备编号" :label-width="formLabelWidth">
                <el-input class="input" clearable placeholder="请输入设备编号" v-model="formSave.serial_number"
                  autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item prop="name" label="请输入设备名称" :label-width="formLabelWidth">
                <el-input class="input" clearable placeholder="请输入设备名称" v-model="formSave.name"
                  autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSubmit">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { get, post ,postJSON ,del} from '@/utils/request'
export default {
  data() {
    return {
      drawer: false,
      // 提供表单数据对象 --->条件对象 
      form: {},
      // 设备数据 
      deviceData: [],
      formSave:{},
      title: "录入设备信息",
      visible: false,

      rules: {
        serial_number: [
          { required: true, message: '请输入工程编号', trigger: "blur" },
        ],
        name: [
          { required: true, message: '请输入工程名称', trigger: "blur" },
        ],
      },
      params: {
        page: 1,
        pageSize: 10
      },
      formLabelWidth: '120px',

    }
  },
  methods: {
    toEdit(row) {
      this.formSave.serial_number = row.serial_number;
      this.formSave.name = row.name;
      this.formSave.id = row.id;
      this.visible = true;
      this.title = '修改工程信息';
    },
    reset() {
       this.$refs['myForm'].resetFields();
    },
    toSubmit() {
      this.$refs['myForm'].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          let res = await postJSON('/device/saveOrUpdate',this.formSave);
          this.$message({
            type:'success',
            message:res.message
          });
          this.visible = false;
          this.getDevData();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    toDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点了确定按钮 发起删除工程请求 
        let res = await del('/device/deleteById', { id });
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        // 从第一页重新载入数据 
        this.params.page = 1;
        // 重新载入数据 
        this.getDevData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    // toBind(row) {
    //   this.$router.push({
    //     path: '/form/details',
    //     query: row
    //   })
    // },
    // toEdit() {
    //   this.visible = true;
    //   this.title = '修改工程信息';
    // },
    toAdd() {
      // 抽屉显示
      this.visible = true;
      // 修改标题 
      this.title = "录入设备信息";
      // 清空表单数据对象 
      this.formSave = {};
    },
    toSearch() {
      this.params.page = 1;
      this.getDevData();
    },
    async getDevData() {
      let temp = {
        ...this.form,
        ...this.params
      };
      let res = await get('/device/pageQuery', temp);
      // console.log(res, '获取设备响应');
      this.deviceData = res.data.list;
    }
  },
  created() {
    this.getDevData();
  }

}
</script>
<style lang="scss" scoped>
// vue2深度选择器   
::v-deep .el-link--inner {
  margin-left: 5px;
}

.input {
  width: 280px;
}

.select {
  width: 280px;
}

.demo-drawer__footer {
  border-top: 1px solid #ececec;
  padding-top: 10px;
  position: fixed;
  bottom: 50px;
  width: 50%;
  text-align: center;
}
</style>